<?php

     session_start();
     include('common.php');

    if (session_is_registered(MySBB_Admin_username) and $member_permission == 1)
    {

// **  **

         if ($groupper_row['admincp_section'] != 1)
         {
             $SF->error('المعذره .. هذه الميزه غير متاحه لك !');
         }

// **  **

         if ($_GET['add'] == 1)
         {
             if ($_GET['index'] == 1)
             {
                 $SF->Make_cplink_path('<a href="section.php?main=1">الاقسام</a> -> اضافة منتدى');

                 $getmainsection_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "section WHERE main_section='1'");

                 $getmainsection_rows = array();
                 while ($getmainsection_row = $DB->sql_fetch_array($getmainsection_query))
                 {
                        $getmainsection_rows[] = $getmainsection_row;
                        $Smarty->assign('getmainsection_rows',$getmainsection_rows);
                 }

                 $getsection_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "section WHERE main_section<>'1' AND sub_section<>'1'");

                 $getsection_rows = array();
                 while ($getsection_row = $DB->sql_fetch_array($getsection_query))
                 {
                        $getsection_rows[] = $getsection_row;
                        $Smarty->assign('getsection_rows',$getsection_rows);
                 }

                 $Smarty->display('section_add.tpl');
             }

             if ($_GET['start'] == 1)
             {
                 if (empty($_POST['T1']) or empty($_POST['T2']))
                 {
                     $SF->error('يرجى تعبئة كافة المعلومات');
                 }

                 if ($_POST['D1'] == 0 and $_POST['D3'] != 1)
                 {
                     $SF->error('يرجى اختيار تحت اي قسم رئيسي يقع هذا القسم , او اختيار (نعم) اذا كان القسم عباره عن قسم رئيسي');
                 }

                 $insert    = $DB->sql_query("INSERT INTO " . $db_prefix . "section(id,title,section_describe,main_section,from_main_section,sort,sub_section,from_sub_section,section_password,show_sig,usesmartcode_allow,section_picture,sectionpicture_type,linksite,subject_order,use_section_picture,hide_subject,linksection,sec_section,sig_iteration) VALUES('NULL','" . $_POST['T1'] . "','" . nl2br($_POST['S1']) . "','" . $_POST['D2'] . "','" . $_POST['D1'] . "','" . $_POST['T2'] . "','" . $_POST['D3'] . "','" . $_POST['D4'] . "','" . $_POST['section_password'] . "','" . $_POST['show_sig'] . "','" . $_POST['usesmartcode_allow'] . "','" . $_POST['section_picture'] . "','" . $_POST['sectionpicture_type'] . "','" . $_POST['linksite'] . "','" . $_POST['subject_order'] . "','" . $_POST['use_section_picture'] . "','" . $_POST['hide_subject'] . "','" . $_POST['linksection'] . "','" . $_POST['sec_section'] . "','" . $_POST['sig_iteration'] . "')");
                 $sectionid = $DB->sql_insert_id($insert);

                 if ($insert)
                 {
                     $groupinfo_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "group ORDER BY id ASC");

                     while ($groupinfo_row = $DB->sql_fetch_array($groupinfo_query))
                     {
                         $insert_group = $DB->sql_query("INSERT INTO " . $db_prefix . "sectiongroup
                                         (id,section_id,group_id,view_section,download_attach,write_subject,write_reply,upload_attach,edit_own_subject,edit_own_reply,del_own_subject,del_own_reply,write_poll,vote_poll)
                                         VALUES ('NULL','" . $sectionid . "','" . $groupinfo_row['id'] . "','" . $groupinfo_row['view_section'] . "','" . $groupinfo_row['download_attach'] . "','" . $groupinfo_row['write_subject'] . "','" . $groupinfo_row['write_reply'] . "','" . $groupinfo_row['upload_attach'] . "','" . $groupinfo_row['edit_own_subject'] . "','" . $groupinfo_row['edit_own_reply'] . "','" . $groupinfo_row['del_own_subject'] . "','" . $groupinfo_row['del_own_reply'] . "','" . $groupinfo_row['write_poll'] . "','" . $groupinfo_row['vote_poll'] . "')");
                     }

                     $updateforums       =         $SF->UpdateForumsArray();
                     $updategroup        =         $SF->UpdateSectionGroupArray();
                     if ($updateforums and $updategroup)
                     {
                         $SF->msg('تم اضافة القسم بنجاح !');
                         $SF->go_to('section.php?main=1',2);
                     }
                 }
             }
         }

// **  **

         if ($_GET['mainadd'] == 1)
         {
             if ($_GET['index'] == 1)
             {
                 $SF->Make_cplink_path('<a href="section.php?main=1">الاقسام</a> -> اضافة قسم رئيسي');
                 $Smarty->display('mainsection_add.tpl');
             }

             if ($_GET['start'] == 1)
             {
                 if (empty($_POST['T1']) or empty($_POST['T2']))
                 {
                     $SF->error('يرجى تعبئة كافة المعلومات');
                 }

                 $insert    = $DB->sql_query("INSERT INTO " . $db_prefix . "section(id,title,main_section,sort) VALUES('NULL','" . $_POST['T1'] . "','1','" . $_POST['T2'] . "')");
                 $sectionid = $DB->sql_insert_id($insert);
                 if ($insert)
                 {
                     $groupinfo_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "group ORDER BY id ASC");

                     while ($groupinfo_row = $DB->sql_fetch_array($groupinfo_query))
                     {
                         $insert_group = $DB->sql_query("INSERT INTO " . $db_prefix . "sectiongroup
                                         (id,section_id,group_id,view_section,main_section)
                                         VALUES ('NULL','" . $sectionid . "','" . $groupinfo_row['id'] . "','" . $groupinfo_row['view_section'] . "','1')");
                     }

                     $updateforums       =         $SF->UpdateForumsArray();
                     $updategroup        =         $SF->UpdateSectionGroupArray();

                     if ($updateforums and $updategroup)
                     {
                         $SF->msg('تم اضافة القسم بنجاح !');
                         $SF->go_to('section.php?main=1',2);
                     }
                 }
             }
         }

// **  **

         if ($_GET['subadd'] == 1)
         {
             if ($_GET['index'] == 1)
             {
                 $getsection_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "section WHERE main_section<>'1' AND sub_section<>'1'");

                 $getsection_rows = array();
                 while ($getsection_row = $DB->sql_fetch_array($getsection_query))
                 {
                        $getsection_rows[] = $getsection_row;
                        $Smarty->assign('getsection_rows',$getsection_rows);
                 }

                 $SF->Make_cplink_path('<a href="section.php?main=1">الاقسام</a> -> اضافة منتدى فرعي');
                 $Smarty->display('subsection_add.tpl');
             }

             if ($_GET['start'] == 1)
             {
                 if (empty($_POST['T1']) or empty($_POST['T2']))
                 {
                     $SF->error('يرجى تعبئة كافة المعلومات');
                 }

                 if ($_POST['D4'] == 0)
                 {
                     $SF->error('يجب اختيار القسم الرئيسي');
                 }

                 $insert    = $DB->sql_query("INSERT INTO " . $db_prefix . "section(id,title,section_describe,main_section,from_main_section,sort,sub_section,from_sub_section,section_password,show_sig,usesmartcode_allow,section_picture,sectionpicture_type,linksite,subject_order,use_section_picture,hide_subject,linksection,sig_iteration) VALUES('NULL','" . $_POST['T1'] . "','" . nl2br($_POST['S1']) . "','0','0','" . $_POST['T2'] . "','1','" . $_POST['D4'] . "','" . $_POST['section_password'] . "','" . $_POST['show_sig'] . "','" . $_POST['usesmartcode_allow'] . "','" . $_POST['section_picture'] . "','" . $_POST['sectionpicture_type'] . "','" . $_POST['linksite'] . "','" . $_POST['subject_order'] . "','" . $_POST['use_section_picture'] . "','" . $_POST['hide_subject'] . "','" . $_POST['linksection'] . "','" . $_POST['sig_iteration'] . "')");
                 $sectionid = $DB->sql_insert_id($insert);

                 if ($insert)
                 {
                     $groupinfo_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "group ORDER BY id ASC");

                     while ($groupinfo_row = $DB->sql_fetch_array($groupinfo_query))
                     {
                         $insert_group = $DB->sql_query("INSERT INTO " . $db_prefix . "sectiongroup
                                         (id,section_id,group_id,view_section,download_attach,write_subject,write_reply,upload_attach,edit_own_subject,edit_own_reply,del_own_subject,del_own_reply,write_poll,vote_poll)
                                         VALUES ('NULL','" . $sectionid . "','" . $groupinfo_row['id'] . "','" . $groupinfo_row['view_section'] . "','" . $groupinfo_row['download_attach'] . "','" . $groupinfo_row['write_subject'] . "','" . $groupinfo_row['write_reply'] . "','" . $groupinfo_row['upload_attach'] . "','" . $groupinfo_row['edit_own_subject'] . "','" . $groupinfo_row['edit_own_reply'] . "','" . $groupinfo_row['del_own_subject'] . "','" . $groupinfo_row['del_own_reply'] . "','" . $groupinfo_row['write_poll'] . "','" . $groupinfo_row['vote_poll'] . "')");
                     }

                     $updateforums            =         $SF->UpdateForumsArray();
                     $updategroup             =         $SF->UpdateSectionGroupArray();
                     $updatesubforums         =         $SF->UpdateSubForums();
                     if ($updateforums and $updategroup and $updatesubforums)
                     {
                         $SF->msg('تم اضافة القسم بنجاح !');
                         $SF->go_to('section.php?main=1',2);
                     }
                 }
             }
         }

// **  **

         if ($_GET['main'] == 1)
         {
             $SF->Make_cplink_path('الاقسام');

             $getmainsection_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "section WHERE main_section='1' ORDER BY sort ASC");

             $getmainsection_rows = array();
             while ($getmainsection_row = $DB->sql_fetch_array($getmainsection_query))
             {
                 $getmainsection_rows[] = $getmainsection_row;
                 $Smarty->assign('getmainsection_rows',$getmainsection_rows);
             }

             $getsection_rows = array();

             $getsubsection_query = $DB->sql_query("SELECT DISTINCT from_sub_section FROM " . $db_prefix . "section WHERE sub_section='1'");
             while ($getsubsection_row = $DB->sql_fetch_array($getsubsection_query))
             {
                 $getsection_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "section WHERE id='" . $getsubsection_row['from_sub_section'] . "'");

                 while ($getsection_row = $DB->sql_fetch_array($getsection_query))
                 {
                     $getsection_rows[] = $getsection_row;
                     $Smarty->assign('getsection_rows',$getsection_rows);
                 }
             }

             $Smarty->display('section_main.tpl');
         }

// **  **

         if ($_GET['cp'] == 1)
         {
             if ($_GET['update_section'] == 1)
             {
                 $getsection_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "section WHERE main_section<>'1' AND from_main_section='" . intval($_GET['id']) . "'");

                 while ($getsection_row = $DB->sql_fetch_array($getsection_query))
                 {
                     $update = $DB->sql_query("UPDATE " . $db_prefix . "section SET sort='" . $_POST[$getsection_row['id']] . "' WHERE id='" . $getsection_row['id'] . "'");
                 }

                 if ($update)
                 {
                     $updateforums            =         $SF->UpdateForumsArray();
                     $updatesubforums         =         $SF->UpdateSubForums();
                     if ($updateforums and $updatesubforums)
                     {
                         $SF->msg('تم التحديث بنجاح !');
                         $SF->go_to($_SERVER['HTTP_REFERER'],2);
                     }
                 }
             }

             if ($_GET['update_main_section'] == 1)
             {
                 $getmainsection_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "section WHERE main_section='1' ORDER BY id DESC");

                 while ($getmainsection_row = $DB->sql_fetch_array($getmainsection_query))
                 {
                     $update = $DB->sql_query("UPDATE " . $db_prefix . "section SET sort='" . $_POST[$getmainsection_row['id']] . "' WHERE id='" . $getmainsection_row['id'] . "'");
                 }

                 if ($update)
                 {

                     $updateforums         =         $SF->UpdateForumsArray();
                     if ($updateforums)
                     {
                         $SF->msg('تم التحديث بنجاح !');
                         $SF->go_to('section.php?main=1',2);
                     }
                 }
             }

             if ($_GET['edit'] == 1)
             {
                 if ($_GET['index'] == 1)
                 {
                     $id = intval($_GET['id']);

                     $getsectioninfo_query  = $DB->sql_query("SELECT * FROM " . $db_prefix . "section WHERE id='$id'");
                     $getsectioninfo_num           = $DB->sql_num_rows($getsectioninfo_query);
                     $getsectioninfo_row    = $DB->sql_fetch_array($getsectioninfo_query);

                     $getmainsection_query  = $DB->sql_query("SELECT * FROM " . $db_prefix . "section WHERE id='" . $getsectioninfo_row['from_main_section'] . "'");
                     $getmainsection_row           = $DB->sql_fetch_array($getmainsection_query);

                     $getmainsection_query2 = $DB->sql_query("SELECT * FROM " . $db_prefix . "section WHERE main_section='1'");

                     $getmainsection_row2s = array();
                     while ($getmainsection_row2 = $DB->sql_fetch_array($getmainsection_query2))
                     {
                         $getmainsection_row2s[] = $getmainsection_row2;
                         $Smarty->assign('getmainsection_row2s',$getmainsection_row2s);
                     }

                     $SF->Make_cplink_path('<a href="section.php?main=1">الاقسام</a> -> تحرير القسم : ' . htmlspecialchars($getsectioninfo_row['title']));

                     if ($getsectioninfo_num <= 0)
                     {
                         $SF->error('القسم المطلوب غير موجود !');
                     }

                     $Smarty->assign_by_ref('section_row',$getsectioninfo_row);
                     $Smarty->assign_by_ref('mainsection_row',$getmainsection_row);

                     $getsection_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "section WHERE main_section<>'1' AND sub_section<>'1'");

                     $getsection_rows = array();
                     while ($getsection_row = $DB->sql_fetch_array($getsection_query))
                     {
                         $getsection_rows[] = $getsection_row;
                         $Smarty->assign('getsection_rows',$getsection_rows);
                     }

                     if ($getsectioninfo_row['sub_section'] == 1)
                     {
                         $getsubsection_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "section WHERE id='" . $getsectioninfo_row['from_sub_section'] . "'");
                         $getsubsection_row   = $DB->sql_fetch_array($getsubsection_query);

                         $Smarty->assign_by_ref('getsubsection_row',$getsubsection_row);
                     }

                     $Smarty->display('section_edit.tpl');
                 }

                 if ($_GET['start'] == 1)
                 {
                     $id = intval($_GET['id']);

                     if (empty($_POST['T1']) or empty($_POST['T2']))
                     {
                         $SF->error('يرجى تعبئة كافة المعلومات');
                     }

                     if ($_POST['D1'] == 0 and $_POST['D2'] == 0)
                     {
                         $SF->error('يرجى اختيار تحت اي قسم رئيسي يقع هذا القسم , او اختيار (نعم) اذا كان القسم عباره عن قسم رئيسي');
                     }

                     if ($_POST['D1'] != 0 and $_POST['D2'] != 0)
                     {
                         $SF->error('لا يجوز ان يكون القسم رئيسي و يكون تحت قسم رئيسي معين في آن واحد');
                     }

                     if ($_POST['D2'] != 0 and $_POST['linksection'] == 1)
                     {
                         $SF->error('لا يمكن ان يكون القسم الرئيسي عباره عن وصله');
                     }

                     if ($_POST['sec_section'] == 1)
                     {
                         $update_subject = $DB->sql_query("UPDATE " . $db_prefix . "subject SET sec_subject='1' WHERE section='" . $id . "'");
                     }

                     if ($_POST['sec_section'] != 1)
                     {
                         $update_subject = $DB->sql_query("UPDATE " . $db_prefix . "subject SET sec_subject='0' WHERE section='" . $id . "'");
                     }

                     $sectionpicture_type = $_POST['sectionpicture_type'];

                     if ($_POST['use_section_picture'] == 0)
                     {
                         $sectionpicture_type = 2;
                     }
                     elseif ($_POST['use_section_picture'] == 1)
                     {
                         $sectionpicture_type = $_POST['sectionpicture_type'];
                     }

                     $update = $DB->sql_query("UPDATE " . $db_prefix . "section SET title='" . $_POST['T1'] . "',section_describe='" . $_POST['S1'] . "',main_section='" . $_POST['D2'] . "',from_main_section='" . $_POST['D1'] . "',sort='" . $_POST['T2'] . "',section_password='" . $_POST['section_password'] . "',show_sig='" . $_POST['show_sig'] . "',sig_iteration='" . $_POST['sig_iteration'] . "',usesmartcode_allow='" . $_POST['usesmartcode_allow'] . "',section_picture='" . $_POST['section_picture'] . "',sectionpicture_type='" . $sectionpicture_type . "',linksection='" . $_POST['linksection'] . "',linksite='" . $_POST['linksite'] . "',subject_order='" . $_POST['subject_order'] . "',sub_section='" . $_POST['D3'] . "',from_sub_section='" . $_POST['D4'] . "',use_section_picture='" . $_POST['use_section_picture'] . "',hide_subject='" . $_POST['hide_subject'] . "',sec_section='" . $_POST['sec_section'] . "' WHERE id='" . $id . "'");
                     if ($update)
                     {
                         $updateforums            =         $SF->UpdateForumsArray();
                         $updatesubforums         =         $SF->UpdateSubForums();

                         if ($updateforums and $updatesubforums)
                         {
                             $SF->msg('تم التحديث بنجاح !');
                             $SF->go_to('section.php?main=1',2);
                         }
                     }
                 }
             }

             if ($_GET['edit_main'] == 1)
             {
                 if ($_GET['index'] == 1)
                 {
                     if (empty($_GET['id']))
                     {
                         $SF->error('الرابط المتبع غير صحيح !');
                     }

                     $id = intval($_GET['id']);

                     $getmaininfo_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "section WHERE id='" . $id . "'");
                     $getmaininfo_row   = $DB->sql_fetch_array($getmaininfo_query);

                     $Smarty->assign_by_ref('getmaininfo_row',$getmaininfo_row);
                     $Smarty->display('mainsection_edit.tpl');
                 }

                 if ($_GET['start'] == 1)
                 {
                     if (empty($_GET['id']))
                     {
                         $SF->error('الرابط المتبع غير صحيح !');
                     }

                     $id = intval($_GET['id']);

                     $update = $DB->sql_query("UPDATE " . $db_prefix . "section SET title='" . $_POST['T1'] . "',sort='" . $_POST['T2'] . "' WHERE id='" . $id . "'");
                     if ($update)
                     {
                         $updateforums         =         $SF->UpdateForumsArray();
                         if ($updateforums)
                         {
                             $SF->msg('تم التحديث بنجاح !');
                             $SF->go_to('section.php?main=1',2);
                         }
                     }
                 }
             }

             if ($_GET['edit_sub'] == 1)
             {
                 if ($_GET['index'] == 1)
                 {
                     if (empty($_GET['id']))
                     {
                         $SF->error('الرابط المتبع غير صحيح !');
                     }

                     $id = intval($_GET['id']);

                     $getsubinfo_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "section WHERE id='" . $id . "'");
                     $getsubinfo_row   = $DB->sql_fetch_array($getsubinfo_query);

                     $getmainsection_query  = $DB->sql_query("SELECT * FROM " . $db_prefix . "section WHERE id='" . $getsubinfo_row['from_sub_section'] . "'");
                     $getmainsection_row           = $DB->sql_fetch_array($getmainsection_query);

                     $getmainsection_query2 = $DB->sql_query("SELECT * FROM " . $db_prefix . "section WHERE id<>'" . $getsubinfo_row['from_sub_section'] . "'");

                     $getmainsection_row2s = array();
                     while ($getmainsection_row2 = $DB->sql_fetch_array($getmainsection_query2))
                     {
                            $getmainsection_row2s[] = $getmainsection_row2;
                            $Smarty->assign('getmainsection_row2s',$getmainsection_row2s);
                     }

                     $SF->Make_cplink_path('<a href="section.php?main=1">الاقسام</a> -> تحرير القسم : ' . $getsubinfo_row['title']);

                     $Smarty->assign_by_ref('section_row',$getsectioninfo_row);
                     $Smarty->assign_by_ref('getmainsection_row',$getmainsection_row);

                     $getsection_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "section WHERE main_section<>'1' AND sub_section<>'1'");

                     $getsection_rows = array();
                     while ($getsection_row = $DB->sql_fetch_array($getsection_query))
                     {
                            $getsection_rows[] = $getsection_row;
                            $Smarty->assign('getsection_rows',$getsection_rows);
                     }

                     if ($getsectioninfo_row['sub_section'] == 1)
                     {
                         $getsubsection_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "section WHERE id='" . $getsubinfo_row['from_sub_section'] . "'");
                         $getsubsection_row   = $DB->sql_fetch_array($getsubsection_query);

                         $Smarty->assign_by_ref('getsubsection_row',$getsubsection_row);
                     }

                     $Smarty->assign_by_ref('getsubinfo_row',$getsubinfo_row);

                     $Smarty->display('subsection_edit.tpl');
                 }

                 if ($_GET['start'] == 1)
                 {
                     if (empty($_POST['T1']) or empty($_POST['T2']))
                     {
                         $SF->error('يرجى تعبئة كافة المعلومات');
                     }

                     $id = intval($_GET['id']);

                     $update = $DB->sql_query("UPDATE " . $db_prefix . "section SET title='" . $_POST['T1'] . "',section_describe='" . $_POST['S1'] . "',sort='" . $_POST['T2'] . "',section_password='" . $_POST['section_password'] . "',show_sig='" . $_POST['show_sig'] . "',sig_iteration='" . $_POST['sig_iteration'] . "',usesmartcode_allow='" . $_POST['usesmartcode_allow'] . "',section_picture='" . $_POST['section_picture'] . "',sectionpicture_type='" . $_POST['sectionpicture_type'] . "',linksection='" . $_POST['linksection'] . "',linksite='" . $_POST['linksite'] . "',subject_order='" . $_POST['subject_order'] . "',from_sub_section='" . $_POST['D4'] . "',use_section_picture='" . $_POST['use_section_picture'] . "',hide_subject='" . $_POST['hide_subject'] . "' WHERE id='" . $id . "'");
                     if ($update)
                     {

                         $updateforums            =         $SF->UpdateForumsArray();
                         $updatesubforums         =         $SF->UpdateSubForums();

                         if ($updateforums)
                         {
                             $SF->msg('تم التحديث بنجاح !');
                             $SF->go_to('section.php?main=1',2);
                         }
                     }
                 }
             }

             if ($_GET['del'] == 1)
             {
                 if ($_GET['sure'] == 1)
                 {
                     $getsection_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "section WHERE main_section<>'1' AND id<>'" . intval($_GET['id']) . "' ORDER BY from_main_section DESC");

                     $SF->html('</td></tr></table><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr><td valign="top">');
                     $SF->html('<form method="POST" action="section.php?cp=1&del=1&sure=2&id=' . intval($_GET['id']). '">');
                     $SF->html("<p dir='rtl'>هل متأكد من حذف هذا القسم ؟<br>");
                     $SF->html('ماذا بالنسبه للمواضيع ؟ :');
                     $SF->html('<select size="1" name="D1" style="font-family: Tahoma; font-size: 8pt" dir="rtl">');
                     $SF->html('<option value="1">حذف جميع المواضيع الموجوده في القسم</option>');
                     $SF->html('<option selected value="2">نقل جميع المواضيع إلى</option>');
                     $SF->html('</select> ');
                     $SF->html('<select size="1" name="D2" style="font-family: Tahoma; font-size: 8pt" dir="rtl">');
                     $SF->html('<option selected value="0">[القسم]</option>');

                     while ($getsection_row = $DB->sql_fetch_array($getsection_query))
                     {
                         $SF->html('<option value="' . $getsection_row['id'] . '">' . $getsection_row['title'] . '</option>');
                     }

                     $SF->html('</select><br>');
                     $SF->html('<input type="submit" value="استمرار" name="B1" style="font-family: Tahoma; font-size: 8pt"></p>');
                     $SF->html('</form>');
                 }

                 if ($_GET['sure'] == 2)
                 {
                     if ($_POST['D1'] == 2 and $_POST['D2'] == 0)
                     {
                         $SF->error('يرجى اختيار القسم');
                     }

                     if ($_POST['D1'] == 1)
                     {
                         $del = $DB->sql_query("DELETE FROM " . $db_prefix . "subject WHERE section='" . intval($_GET['id']) . "'");

                         $getsubsection_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "section WHERE from_sub_section ='" . intval($_GET['id']) . "'");

                     while ($getsubsection_row = $DB->sql_fetch_array($getsubsection_query))
                     {
                         $del = $DB->sql_query("DELETE FROM " . $db_prefix . "subject WHERE section='" . $getsubsection_row['id'] . "'");
                         $del = $DB->sql_query("DELETE FROM " . $db_prefix . "sectiongroup WHERE section_id='" . $getsubsection_row['id'] . "'");
                     }

                         if ($del)
                         {
                             $del_section  = $DB->sql_query("DELETE FROM " . $db_prefix . "section WHERE from_sub_section='" . intval($_GET['id']) . "'");
                             $del_section  = $DB->sql_query("DELETE FROM " . $db_prefix . "section WHERE id='" . intval($_GET['id']) . "'");
                             $del_gsection = $DB->sql_query("DELETE FROM " . $db_prefix . "sectiongroup WHERE section_id='" . intval($_GET['id']) . "'");

                             if ($del_section)
                             {
                                 $updateforums         =         $SF->UpdateForumsArray();
                                 $updategroup             =         $SF->UpdateSectionGroupArray();
                                 $updatesubforums         =         $SF->UpdateSubForums();
                                 if ($updateforums)
                                 {
                                     $SF->msg('تم حذف القسم بنجاح !');
                                     $SF->go_to('section.php?main=1',2);
                                 }
                             }
                         }
                     }

                     if ($_POST['D1'] == 2)
                     {
                         $update = $DB->sql_query("UPDATE " . $db_prefix . "subject SET section='" . intval($_POST['D2']) . "' WHERE section='" . intval($_GET['id']) . "'");
                         if ($update)
                         {

                             $getsubsection_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "section WHERE from_sub_section ='" . intval($_GET['id']) . "'");
                             $update = $DB->sql_query("UPDATE " . $db_prefix . "section SET from_sub_section='" . intval($_POST['D2']) . "' WHERE from_sub_section='" . intval($_GET['id']) . "'");

                             $del_section = $DB->sql_query("DELETE FROM " . $db_prefix . "section WHERE id='" . intval($_GET['id']) . "'");
                             if ($del_section)
                             {
                                 $updateforums            =         $SF->UpdateForumsArray();
                                 $updatesubforums         =         $SF->UpdateSubForums();
                                 if ($updateforums and $updatesubforums)
                                 {
                                     $SF->msg('تم حذف القسم بنجاح !');
                                     $SF->go_to('section.php?main=1',2);
                                 }
                             }
                         }
                     }
                 }
             }

             if ($_GET['del'] == 2)
             {
                 if ($_GET['sure'] == 1)
                 {
                     $getsection_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "section WHERE main_section='1' AND id<>'" . intval($_GET['id']) . "' ORDER BY from_main_section DESC");

                     $SF->html('</td></tr></table><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr><td valign="top">');
                     $SF->html('<form method="POST" action="section.php?cp=1&del=2&sure=2&id=' . intval($_GET['id']) . '">');
                     $SF->html("<p dir='rtl'>هل متأكد من حذف هذا القسم ؟<br>");
                     $SF->html("ماذا بالنسبه للاقسام التي تقع تحته :");
                     $SF->html('<select size="1" name="D1" style="font-family: Tahoma; font-size: 8pt" dir="rtl">');
                     $SF->html('<option value="1">حذف جميع الاقسام الموجوده تحت هذا القسم</option>');
                     $SF->html('<option selected value="2">نقل جميع الاقسام إلى</option>');
                     $SF->html('</select> ');
                     $SF->html('<select size="1" name="D2" style="font-family: Tahoma; font-size: 8pt" dir="rtl">');
                     $SF->html('<option selected value="0">[القسم]</option>');

                     while ($getsection_row = $DB->sql_fetch_array($getsection_query))
                     {
                         $SF->html('<option value="' . $getsection_row['id'] . '">' . $getsection_row['title'] . '</option>');
                     }

                     $SF->html('</select><br>');
                     $SF->html('<input type="submit" value="استمرار" name="B1" style="font-family: Tahoma; font-size: 8pt"></p>');
                     $SF->html('</form>');
                 }

                 if ($_GET['sure'] == 2)
                 {
                     if ($_POST['D1'] == 2 and $_POST['D2'] == 0)
                     {
                         $SF->error('يرجى اختيار القسم');
                     }

                     if ($_POST['D1'] == 1)
                     {
                         $del  = $DB->sql_query("DELETE FROM " . $db_prefix . "section WHERE id='" . intval($_GET['id']) . "'");
                         $del2 = $DB->sql_query("DELETE FROM " . $db_prefix . "subject WHERE section='" . intval($_GET['id']) . "'");

                     $getsubsection_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "section WHERE from_main_section ='" . intval($_GET['id']) . "'");

                     while ($getsubsection_row = $DB->sql_fetch_array($getsubsection_query))
                     {
                         $del = $DB->sql_query("DELETE FROM " . $db_prefix . "subject WHERE section='" . $getsubsection_row['id'] . "'");

                         $getsubsection2_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "section WHERE from_sub_section ='" . $getsubsection_row['id'] . "'");

                         while ($getsubsection2_row = $DB->sql_fetch_array($getsubsection2_query))
                         {
                             $del = $DB->sql_query("DELETE FROM " . $db_prefix . "subject WHERE section='" . $getsubsection2_row['id'] . "'");
                             $del = $DB->sql_query("DELETE FROM " . $db_prefix . "sectiongroup WHERE section_id='" . $getsubsection2_row['id'] . "'");
                             $del = $DB->sql_query("DELETE FROM " . $db_prefix . "section WHERE id='" . $getsubsection2_row['id'] . "'");
                         }
                         $del = $DB->sql_query("DELETE FROM " . $db_prefix . "sectiongroup WHERE section_id='" . $getsubsection_row['id'] . "'");
                         $del = $DB->sql_query("DELETE FROM " . $db_prefix . "section WHERE from_main_section='" . intval($_GET['id']) . "'");
                     }

                         if ($del2)
                         {
                             $del_section = $DB->sql_query("DELETE FROM " . $db_prefix . "section WHERE id='" . intval($_GET['id']) . "'");
                             if ($del_section)
                             {
                                 $updateforums         =         $SF->UpdateForumsArray();
                                 if ($updateforums)
                                 {
                                     $SF->msg('تم حذف القسم بنجاح !');
                                     $SF->go_to('section.php?main=1',2);
                                 }
                             }
                         }
                     }

                     if ($_POST['D1'] == 2)
                     {
                         $update = $DB->sql_query("UPDATE " . $db_prefix . "section SET from_main_section='" . intval($_POST['D2']) . "' WHERE from_main_section='" . intval($_GET['id']) . "'");
                         if ($update)
                         {
                             $del_section = $DB->sql_query("DELETE FROM " . $db_prefix . "section WHERE id='" . intval($_GET['id']) . "'");
                             if ($del_section)
                             {
                                 $updateforums         =         $SF->UpdateForumsArray();
                                 if ($updateforums)
                                 {
                                     $SF->msg('تم حذف القسم بنجاح !');
                                     $SF->go_to('section.php?main=1',2);
                                 }
                             }
                         }
                     }
                 }
             }
         }

// **  **

         if ($_GET['section'] == 1)
         {
             $id = intval($_GET['id']);

             $getmainsectioninfo_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "section WHERE id='$id' AND main_section='1'");
             $getmainsectioninfo_num   = $DB->sql_num_rows($getmainsectioninfo_query);

             if ($getmainsectioninfo_num <= 0)
             {
                 $SF->error('المعذره .. القسم المطلوب غير موجود !');
             }

             $getsection_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "section WHERE main_section<>'1' AND from_main_section='$id' ORDER BY sort ASC");

             $getsection_rows = array();
             while ($getsection_row = $DB->sql_fetch_array($getsection_query))
             {
                 $getsection_rows[] = $getsection_row;
                 $Smarty->assign('getsection_rows',$getsection_rows);
             }

             $SF->Make_cplink_path('<a href="section.php?main=1">الاقسام الرئيسيه</a> -> الاقسام');

             $Smarty->assign('id',$id);
             $Smarty->display('section_show.tpl');
         }

// **  **

         if ($_GET['sub_section'] == 1)
         {
             $id = intval($_GET['id']);

             $getmainsectioninfo_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "section WHERE id='$id'");
             $getmainsectioninfo_num   = $DB->sql_num_rows($getmainsectioninfo_query);

             if ($getmainsectioninfo_num <= 0)
             {
                 $SF->error('المعذره .. القسم المطلوب غير موجود !');
             }

             $getsection_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "section WHERE from_sub_section='$id' ORDER BY sort ASC");

             $getsection_rows = array();
             while ($getsection_row = $DB->sql_fetch_array($getsection_query))
             {
                 $getsection_rows[] = $getsection_row;
                 $Smarty->assign('getsection_rows',$getsection_rows);
             }

             $SF->Make_cplink_path('<a href="section.php?main=1">الاقسام الرئيسيه</a> -> الاقسام');

             $Smarty->assign('id',$id);
             $Smarty->display('sub_section_show.tpl');
         }

// **  **

    }

?>